<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/10/28
 * Time: 15:13
 */

namespace app\adminapiv3\service;


class ServerUser extends Base
{
    /**
     * @name 列表
     * @param $params
     * @param int $limit
     * @return array
     * @author wx
     * @date 2019/10/28 15:43
     */
    function list($params, $limit = 10)
    {
        $server = new \app\common\service\ServerUser();
        $list = $server->list($params, $limit);

        foreach ($list['data'] as $k => $v){
            $v['ctime'] = date('Y-m-d H:i:s',$v['ctime']);
            if($v['last_login_time']){
                $v['last_login_time'] = date('Y-m-d H:i:s',$v['last_login_time']);
            }else{
                $v['last_login_time'] = '暂无最近登录时间';
            }
            $list['data'][$k] = $v;
        }

        return $list;

        /*$userInfo = $this->getUserInfo();
        if ($userInfo['level'] == 1) {
            $list = $db->order('id desc')->field($field)
                ->where('level',0)
                ->where('status',1)
                ->paginate($limit)->toArray();
        }else{
            $list = $db->order('id desc')->field($field)
                ->where('level',2)
                ->where('status',1)
                ->paginate($limit)->toArray();
        }*/
    }

    /**
     * @name 添加
     * @param $addData
     * @return bool
     * @author wx
     * @date 2019/10/28 15:50
     */
    function add($addData)
    {
        //检查重复
        if ($this->checkExist(['real_name' => $addData['real_name']])) {
            $this->setError($this->CODE_FAIL, '请勿添加重复账号');
            return false;
        }

        $addData['reg_ip'] = $_SERVER['REMOTE_ADDR'];
        $addData['last_login_ip'] = '';
        $addData['last_login_time'] = '';
        $addData['ctime'] = time();
        $addData['level'] = 2;
        $addData['salt'] = createSalt(6);

        $serverPassword = new Password();
        $addData['password'] = $serverPassword->encrypt($addData['password'], $addData['salt']);

        $server = new \app\common\service\ServerUser();
        $server->add($addData);

        return true;
    }

    function modify($id, $showName, $password)
    {
        if(!empty($password)){
            $updateData['salt'] = createSalt(6);
            $serverPassword = new Password();
            $updateData['password'] = $serverPassword->encrypt($password, $updateData['salt']);
        }

        $updateData['show_name'] = $showName;

        $server = new \app\common\service\ServerUser();
        $server->modify(['id' => $id], $updateData);

        return true;
    }

    function del($id)
    {
        $server = new \app\common\service\ServerUser();
        //超管不能删除
        $where[] = array('id', 'in', $id);
        $where[] = array('level', '=', '1');
        
        if ($this->checkExist($where)) {
            $this->setError($this->CODE_FAIL,'超级管理员不能删除!');
            return false;
        }

        $condition[] = array('id', 'in', $id);
        $server->del($condition);

        return true;
    }

    /**
     * @name 修改当前用户密码
     * @author wx
     * @date 2019/10/28 16:12
     */
    function modifyUserPassword($oldPassword, $newPassword)
    {
        $user_info = $this->getUserInfo();
        $where['id'] = $user_info['id'];
        $server = new \app\common\service\ServerUser();
        $user = $server->findData($where);

        if (!$user) {
            return $this->setError($this->CODE_FAIL, '该用户不存在');
            return false;
        }

        //验证旧密码是否正确
        $serverPassword = new Password();
        $checkRes = $serverPassword->decrypt($oldPassword, $user['salt'], $user['password']);

        if (!$checkRes) {
            return $this->setError($this->CODE_FAIL, '当前密码不正确');
            return false;
        }

        //修改用户信息
        $updateData['password'] = $serverPassword->encrypt($newPassword, $user['salt']);
        $server->modify(['id' => $user_info['id']], $updateData);

        return true;
    }

    /**
     * @name 验证是否存在
     * @param $condition
     * @author wx
     * @date 2019/10/28 16:20
     */
    function checkExist($condition)
    {
        $server = new \app\common\service\ServerUser();
        $info = $server->findData($condition);

        if(!$info){
            return false;
        }

        return true;
    }

    /**
     * @name 修改超级管理员密码
     * @param $password
     * @return bool
     * @author wx
     * @date 2019/12/23 10:19
     */
    function modifyAdmin($password)
    {
        $userInfo = $this->getUserInfo();

        if($userInfo['level'] != 0){
            $this->setError($this->CODE_FAIL, '您没有权限执行此操作');
            return false;
        }

        $serverPassword = new Password();
        $updateData['salt'] = createSalt(6);
        $updateData['password'] = $serverPassword->encrypt($password, $updateData['salt']);

        $server = new \app\common\service\ServerUser();
        $server->modify(['level' => 1], $updateData);

        return true;
    }


}